//
//  PublicWebVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/5/4.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "PublicWebVC.h"
#import <WebKit/WebKit.h>
@interface PublicWebVC ()<WKUIDelegate,WKNavigationDelegate>
@property (nonatomic ,strong) WKWebView *webView;
@end

@implementation PublicWebVC{
    UIProgressView *progressView;
    BOOL        isSelect;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.url) {
        if (self.isAll) {
            NSURL *url=[NSURL URLWithString:self.url];
            NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
            [self.webView loadRequest:request];
            UIView *view=[[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
            if ([view respondsToSelector:@selector(setBackgroundColor:)]) {
                view.backgroundColor=NAVCOLOR;
            }
            
        }else{
            NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@?pl=1",self.url]];
            NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
            [self.webView loadRequest:request];
        }
        
    }else{
        NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://mobile.c8.cn/Plan/Rule/%ld?pl=1",self.lType]];
        NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url];
        [self.webView loadRequest:request];
    }
    
    progressView = [[UIProgressView alloc]init];
    progressView.progressTintColor=[UIColor whiteColor];
    progressView.trackTintColor=NAVCOLOR;
    [self.view addSubview:progressView];
    [progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(@2);
    }];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (self.isHaddenNav) {
        [self.navigationController setNavigationBarHidden:YES animated:animated];
    }

    self.webView.UIDelegate=self;
    self.webView.navigationDelegate=self;
    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew| NSKeyValueObservingOptionOld context:nil];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    
    if (self.isHaddenNav) {
        
        [self.navigationController setNavigationBarHidden:NO animated:animated];
        
        
    }
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    [self.webView setNavigationDelegate:nil];
    [self.webView setUIDelegate:nil];
}



- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([keyPath isEqual: @"estimatedProgress"] && object == self.webView) {
        [progressView setAlpha:1.0f];
        [progressView setProgress:self.webView.estimatedProgress animated:YES];
        if(self.webView.estimatedProgress >= 1.0f)
        {
            [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
                [progressView setAlpha:0.0f];
            } completion:^(BOOL finished) {
                [progressView setProgress:0.0f animated:NO];
            }];
        }
    }else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}



- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    
    if (webView.title.length > 0) {
        if (self.title.length==0) {
            self.title = webView.title;
        }
    }
    
    // 禁止放大缩小
    NSString *injectionJSString = @"var script = document.createElement('meta');"
    "script.name = 'viewport';"
    "script.content=\"width=device-width, initial-scale=1.0,maximum-scale=1.0, minimum-scale=1.0, user-scalable=no\";"
    "document.getElementsByTagName('head')[0].appendChild(script);";
    [webView evaluateJavaScript:injectionJSString completionHandler:nil];
}

- (WKWebView *)webView{
    if (!_webView) {
        WS(weakSelf);
        //创建网页配置对象
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        // 创建设置对象
        WKPreferences *preference = [[WKPreferences alloc] init];
        // 设置字体大小(最小的字体大小)
//        preference.minimumFontSize = 16;
        // 设置偏好设置对象
        config.preferences = preference;
        _webView=[[WKWebView alloc]initWithFrame:CGRectZero configuration:config];
        _webView.navigationDelegate=self;
        [self.view addSubview:_webView];
        [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf.view);
        }];
//        [self showNavLine];
    }
    return _webView;
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction*)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    
    
    
    NSString *strRequest = [navigationAction.request.URL.absoluteString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    if([strRequest isEqualToString:@"https://9369.cc/"] || [ strRequest isEqualToString:@"https://9369.cc/zst.html"]) {
        
        
        //截获页面里面的链接点击
        //do something you want
        decisionHandler(WKNavigationActionPolicyCancel);//不允许跳转
        [self.navigationController popToViewController:self.saveViewController animated:YES];
        
    } else {
        decisionHandler(WKNavigationActionPolicyAllow);//允许跳转
        
    }
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
